package com.lzs.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lzs.entity.Menu;
import com.lzs.entity.Role;
import com.lzs.system.mapper.MenuMapper;
import com.lzs.system.mapper.RoleMapper;
import com.lzs.system.service.MenuService;
import com.lzs.system.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzs.util.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 刘中帅
 * @since 2020-11-24
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Autowired
    private  RoleMapper roleMapper;
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuService menuService;
    @Override
    public Page<Role> selectAllRolePage(Integer page, Integer limit, Role role) {
        Page<Role> rolePage=new Page<>(page,limit);
        String name = role.getName();
        QueryWrapper<Role> queryWrapper=new QueryWrapper<>();
        if (StringUtils.isNotEmpty(name)){
            queryWrapper.like("name",name);
        }
        roleMapper.selectPage(rolePage,queryWrapper);
        return rolePage;

    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @param role
     * @return
     */
    @Override
    public IPage<Role> selectRolePage(Integer page, Integer limit, Role role) {
        Page<Role> rolePage=new Page<>(page,limit);
        return roleMapper.selectRolePage(rolePage, role);
    }



    @Override
    public R insertOrUpdateRole(Role role) {
        if (role.getRoleId()!=null){
            role.setUpdateTime(new Date());
            int i = roleMapper.updateById(role);
            if (i>0){
                return R.ok().message("修改成功");
            }
            return R.ok().message("修改失败");
        }else{
            role.setCreateTime(new Date());
            int i = roleMapper.insert(role);
            if (i>0){
                return R.ok().message("新增成功");
            }
            return R.ok().message("新增失败");
        }
    }

    /**
     * 修改时回显内容
     * @param roleId
     * @return
     */
    @Override
    public R findByRoleId(Long roleId) {
        Role role = roleMapper.findByRoleId(roleId);
        return R.ok().data("role",role);
    }

    @Override
    public boolean isYeZi(Long menuId, List<Menu> menus) {
        Menu menu = menuMapper.selectById(menuId);
        List<Menu> son=new ArrayList<>();
        if (menu!=null){
            for (Menu menu1:menus){
                if (menu1.getPcode().equals(menu.getCode())){
                    son.add(menu);
                }
            }
            for (Menu menu1:son){
                menu1.setChildren(selectChildMenu(menu1.getMenuId(),menus));
            }
        }
        if (son!=null&&son.size()>0){
            return false;
        }
        return true;
    }

    @Override
    public List<Long> selectMenuByRoleId(Long roleId) {
        return roleMapper.selectMenuByRoleId(roleId);
    }

    // 改变权限
    @Override
    public R updateMenuByRoleId(Long roleId,List<Long> menus) {
        if (menus.size()>0){
            roleMapper.deleteMenuByRoleId(roleId);
            int i = roleMapper.insertMenuByRoleId(menus, roleId);
            if (i>0){
                return R.ok().message("配置成功");
            }
            return R.error().message("配置失败");
        }
        roleMapper.deleteMenuByRoleId(roleId);
        return R.ok().message("配置成功，当前角色无权限");
    }



    @Override
    public List<Role> selectRoleTree() {
        List<Role> roles = roleMapper.selectList(null);
        List<Role> root=new ArrayList<>();
//        遍历出最大的角色0 添加到root中
        for (Role parent:roles){
            if (parent.getPid()==0){
                root.add(parent);
            }
        }
        for (Role parent:root){
            parent.setChildren(selectChild(parent.getRoleId(),roles));
        }
        return root;
    }

    public List<Role> selectChild(Long pid,List<Role> roles){
        List<Role> children=new ArrayList<>();
        for (Role father:roles){
            if (father.getPid().equals(pid)){
                children.add(father);
            }
        }
        for (Role child:children){
            child.setChildren(selectChild(child.getRoleId(),roles));
        }
        return children;
    }

    private List<Menu> selectChildMenu(Long menuId, List<Menu> list) {
        List<Menu> children=new ArrayList<>();
        for(Menu menu:list){
            if(menu.getMenuId().equals(menuId)){
                children.add(menu);
            }
        }
        //遍历所有的children
        for(Menu c:children){
            c.setChildren(selectChildMenu(c.getMenuId(),list));
        }

        return children;
    }

}
